package cn.itcast.handler;

import cn.itcast.message.GroupMembersRequestMessage;
import cn.itcast.message.GroupMembersResponseMessage;
import cn.itcast.server.session.GroupSession;
import cn.itcast.server.session.GroupSessionFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Set;

/**
 * @Name: GroupMembersRequestMessageHandler
 * @Description:
 * @Author: Mr.Tong
 */
@ChannelHandler.Sharable
public class GroupMembersRequestMessageHandler extends SimpleChannelInboundHandler<GroupMembersRequestMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupMembersRequestMessage msg) throws Exception {
        String groupName = msg.getGroupName();

        GroupSession groupSession = GroupSessionFactory.getGroupSession();
        Set<String> members = groupSession.getMembers(groupName);

        ctx.channel().writeAndFlush(new GroupMembersResponseMessage(members));

        //上述代码是所有的客户端都可以查看某一个群聊的所有成员
        //业务逻辑来讲，只有群聊内的成员才可以查看群成员，后续可以进行优化

    }
}
